package com.wannengbang.cloudleader.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.ItemListDTO;
import com.wannengbang.cloudleader.homepage.adapter.NoticeAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.FastClickUtils;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AutoScrollRecyclerView;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.DialogFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 划拨或分配给门店
 */
public class ShowRedEnvelopesDialog extends DialogFragment {

    @BindView(R.id.recyclerView_dynamic)
    AutoScrollRecyclerView recyclerViewDynamic;
    @BindView(R.id.tv_money_count)
    TextView tv_money_count;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private List<ItemListDTO.DataBean.itemList> lists;
    private NoticeAdapter noticeDynamicListAdapter;
    private HomePageModelImpl homePageModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_red_envelopes, null);
        ButterKnife.bind(this, view);
        homePageModel = new HomePageModelImpl();
        mActivity = getActivity();
        initView();
        initData();
        return view;
    }
    private void initData() {
        homePageModel.requestAgentMoneyLogHbLotteryLog(new DataCallBack<ItemListDTO>() {
            @Override
            public void onSuccessful(ItemListDTO bean) {
                String s = new Gson().toJson(bean);

                Log.e("红包",s);
                tv_money_count.setText(NumberFormatUtils.formatTwoDecimal(String.valueOf(bean.getData().getTotal().getMoney_count()/100))+"元");

                lists.clear();
                lists.addAll(bean.getData().getLists());
                noticeDynamicListAdapter.notifyDataSetChanged();

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public static ShowRedEnvelopesDialog newInstance() {
        Bundle args = new Bundle();
        ShowRedEnvelopesDialog fragment = new ShowRedEnvelopesDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {
        lists = new ArrayList<>();
        noticeDynamicListAdapter = new NoticeAdapter(lists);
        recyclerViewDynamic.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerViewDynamic.setAdapter(noticeDynamicListAdapter);
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);

        dialog.setCancelable(true);

        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.iv_close})
    public void onViewClicked(View view) {
        if (FastClickUtils.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.iv_close:
                dialog.dismiss();
                break;
        }
    }

    public interface OnCallBackListener {
        void onCallBack();
    }

    @Override
    public void onStop() {
        super.onStop();
        recyclerViewDynamic.stop();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onStart() {
        super.onStart();
        //流式滚动效果
        recyclerViewDynamic.start();
    }

}

